package com.inspur.incubator.common;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
 




 import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class SecretUtils {	
	     private static final String Algorithm = "DESede";    
	     private static final String PASSWORD_CRYPT_KEY = "xianganhyfd4clkj8a1c85644764568729k";
	    
	    
	     /**
	      * 加密方法
	      * @param src 源数据的字节数组
	      * @return 
	      */
	     public static byte[] encryptMode(byte[] src) {
	         try {
	              SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm);    //生成密钥
	              Cipher c1 = Cipher.getInstance(Algorithm);    
	              c1.init(Cipher.ENCRYPT_MODE, deskey);    
	              return c1.doFinal(src);
	          } catch (java.security.NoSuchAlgorithmException e1) {
	              e1.printStackTrace();
	          } catch (javax.crypto.NoSuchPaddingException e2) {
	              e2.printStackTrace();
	          } catch (java.lang.Exception e3) {
	              e3.printStackTrace();
	         }
	          return null;
	      }
	     
	     
	     /**
	      * 解密函数
	      * @param src 密文的字节数组
	      * @return
	      */
	     public static byte[] decryptMode(byte[] src) {      
	         try {
	             SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm);
	             Cipher c1 = Cipher.getInstance(Algorithm);
	             c1.init(Cipher.DECRYPT_MODE, deskey);    //初始化为解密模式
	             return c1.doFinal(src);
	         } catch (java.security.NoSuchAlgorithmException e1) {
	             e1.printStackTrace();
	         } catch (javax.crypto.NoSuchPaddingException e2) {
	             e2.printStackTrace();
	         } catch (java.lang.Exception e3) {
	             e3.printStackTrace();
	         }
	         return null;
	      }
	     
	     
	     /*
	      * 根据字符串生成密钥字节数组 
	      * @param keyStr 密钥字符串
	      * @return 
	      * @throws UnsupportedEncodingException
	      */
	     public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException{
	         byte[] key = new byte[24];   
	         byte[] temp = keyStr.getBytes("UTF-8");  	        
	         if(key.length > temp.length){	           
	             System.arraycopy(temp, 0, key, 0, temp.length);
	         }else{
	             System.arraycopy(temp, 0, key, 0, key.length);
	         }
	         return key;
	     } 
	     /**
	      * 加密
	      * */
	     public static String getcipher(String msg) throws UnsupportedEncodingException{
	    	 byte[] secretArr = SecretUtils.encryptMode(msg.getBytes("UTF-8"));    	    	       	    	       
	         BASE64Encoder encode = new BASE64Encoder(); 
	         String Str = encode.encode(secretArr);
	         //System.out.println("【加密后Str】：" + Str );
	    	 return Str;
	     }
	     /**
	      * 解密
	     * @throws IOException 
	      * */
	     public static String getorgstr(String Str) throws IOException{
	    	 BASE64Decoder decode = new BASE64Decoder(); 
	         byte[] b=decode.decodeBuffer(Str);
	         byte[] myMsgArr = SecretUtils.decryptMode(b);  
	         //System.out.println("【解密后】：" + new String(myMsgArr,"UTF-8"));
	    	 return new String(myMsgArr,"UTF-8");
	     }
	     public static void main(String[] args) throws IOException {
	    	         String msg = "那么";
	    	         System.out.println("【加密前】：" + msg);	    	        
	    	         //加密
	    	         byte[] secretArr = SecretUtils.encryptMode(msg.getBytes("UTF-8"));    	    	       	    	       
	    	         BASE64Encoder encode = new BASE64Encoder(); 
	    	         String Str = encode.encode(secretArr);
	    	         System.out.println("【加密后Str】：" + Str );
	    	         //解密
	    	         BASE64Decoder decode = new BASE64Decoder(); 
	    	         byte[] b=decode.decodeBuffer(Str);
	    	         byte[] myMsgArr = SecretUtils.decryptMode(b);  
	    	         System.out.println("【解密后】：" + new String(myMsgArr,"UTF-8"));
	    	     }
}
